#include "hdl_pwm.h"

int8_t hdl_pwm_init(uint16_t per_ms)
{
    #if (TIM_PWM_ENABLE == ON)
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_OCInitTypeDef   TIM_OCInitStructure;
    #endif
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

	PWM_GPIO_APBxClock_FUN(RCC_APB2Periph_TIM1,ENABLE);
    TIM_TimeBaseInitStructure.TIM_ClockDivision     = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInitStructure.TIM_Period            = per_ms -1;  //MAX value is 0XFFFF
    TIM_TimeBaseInitStructure.TIM_Prescaler         = 72-1;  //MAX value is 0XFFFF
    TIM_TimeBaseInit(PWM_TIM_TIMx,&TIM_TimeBaseInitStructure);

    #if (TIM_PWM_ENABLE == ON)
        PWM_GPIO_APBxClock_FUN(PWM_GPIO_GPIO_PeriphClock,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Pin = PWM_GPIO_GPIO_PIN;
        GPIO_Init(PWM_GPIO_GPIO_PORT,&GPIO_InitStructure);

        TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
        TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
        TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 200;
        TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
        TIM_OC1Init(PWM_TIM_TIMx,&TIM_OCInitStructure);
        TIM_OC1PreloadConfig(PWM_TIM_TIMx,TIM_OCPreload_Enable);
        TIM_ARRPreloadConfig(PWM_TIM_TIMx,ENABLE);
        TIM_CtrlPWMOutputs(PWM_TIM_TIMx,ENABLE);
    #else
        TIM_ClearFlag(PWM_TIM_TIMx,TIM_FLAG_Update);
        TIM_ITConfig(PWM_TIM_TIMx,TIM_IT_Update,ENABLE);
        USR_NVIC_Config(PWM_TIM_IRQ,0,3);
    #endif
    TIM_Cmd(PWM_TIM_TIMx,ENABLE);
    CHKFILE;
    return SUCCESS;
}

int8_t hdl_pwm_set(uint16_t value)
{
    TIM_SetCompare1(PWM_TIM_TIMx,value);
    DBG_MSG("servo value:%d\r\n",value);
    return SUCCESS;
}

/*
	舵机角度控制
	传入参数：角度
*/
void hdl_servo_angle_ctrl(uint16_t Angle)
{
	uint16_t pluse=0;
	pluse = (uint16_t)((50+Angle*100/90.0)*10);
    // DBG_MSG("Angle:%d pluse:%d\r\n",Angle,pluse);
	hdl_pwm_set(pluse);
}

uint8_t i =0;
uint8_t dir = 0;
void servo_scan(void)
{
    hdl_servo_angle_ctrl(i);
    systick_delay_ms(20);
    if(dir == 0)
    {
        i++;
        if(i>=179)
        {
            dir = 1;
        }
    }
    else
    {
        i--;
        if(i==1)
        {
            dir = 0;
        }
    }
    // DBG_MSG("value:%d,dir:%d\r\n",i,dir);
}

int8_t hdl_pwm_ch2_init(uint16_t per_ms)
{
    #if (TIM_PWM_ENABLE == ON)
        GPIO_InitTypeDef    GPIO_InitStructure;
        TIM_OCInitTypeDef   TIM_OCInitStructure;
    #endif
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

	PWM_TIM2_APBxClock_FUN(PWM_TIM2_CLK,ENABLE);
    TIM_TimeBaseInitStructure.TIM_ClockDivision     = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInitStructure.TIM_Period            = per_ms -1;  //MAX value is 0XFFFF
    TIM_TimeBaseInitStructure.TIM_Prescaler         = 36-1;       //MAX value is 0XFFFF
    TIM_TimeBaseInit(PWM_TIM2_TIMx,&TIM_TimeBaseInitStructure);

    #if (TIM_PWM_ENABLE == ON)
        PWM_CH2_GPIO_APBxClock_FUN(PWM_CH2_GPIO_PeriphClock,ENABLE);
        // RCC_APB1PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Pin = PWM_CH2_GPIO_PIN;
        GPIO_Init(PWM_CH2_GPIO_PORT,&GPIO_InitStructure);

        TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
        TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
        TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 0;//200;
        TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
        TIM_OC2Init(PWM_TIM2_TIMx,&TIM_OCInitStructure);
        TIM_OC2PreloadConfig(PWM_TIM2_TIMx,TIM_OCPreload_Enable);
        TIM_ARRPreloadConfig(PWM_TIM2_TIMx,ENABLE);
        TIM_CtrlPWMOutputs(PWM_TIM2_TIMx,ENABLE);
    #else
        TIM_ClearFlag(PWM_TIM2_TIMx,TIM_FLAG_Update);
        TIM_ITConfig(PWM_TIM2_TIMx,TIM_IT_Update,ENABLE);
        USR_NVIC_Config(PWM_TIM2_TIMx,0,3);
    #endif
    TIM_Cmd(PWM_TIM2_TIMx,ENABLE);
    CHKFILE;
    return SUCCESS;
}

void hdl_led_pwm_set(uint16_t value)
{
    TIM_SetCompare2(PWM_TIM2_TIMx,value);
}


struct hal_pwm_if g_hdl_pwm_if =
{
    .sg90_pwm_init= hdl_pwm_init,
    .sg90_pwm_set = hdl_servo_angle_ctrl,
    .led_pwm_init = hdl_pwm_ch2_init,
    .led_pwm_set = hdl_led_pwm_set,
};



/// @brief ///////////////////////////////////////////////////
/// @param  
void TIM1_UP_IRQHandler(void)
{
    if ( TIM_GetITStatus(PWM_TIM_TIMx, TIM_IT_Update) != RESET )
    {
        LED_GREEN_TOGGLE;
        TIM_ClearITPendingBit(PWM_TIM_TIMx , TIM_FLAG_Update);
    }
}

void TIM2_IRQHandler(void)
{

}
